package com.mtt.erp.freight.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mtt.erp.common.domain.QueryRequest;
import com.mtt.erp.common.exception.MttErpException;
import com.mtt.erp.common.utils.SortUtil;
import com.mtt.erp.freight.dao.McaFileMapper;
import com.mtt.erp.freight.domain.McaFile;
import com.mtt.erp.freight.dto.McaCreateDto;
import com.mtt.erp.freight.dto.McaDto;
import com.mtt.erp.freight.service.IMcaFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @author MrBird
 */
@Service
@Slf4j
public class McaFileServiceImpl extends ServiceImpl<McaFileMapper, McaFile> implements IMcaFileService {

    @Value("${erp.max.batch.insert.num}")
    private int batchInsertMaxNum;

    @Override
    public IPage<McaFile> findMca(QueryRequest request, McaDto mcaDto) {
        LambdaQueryWrapper<McaFile> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(mcaDto.getMca01())) {
            queryWrapper.eq(McaFile::getMca01, mcaDto.getMca01());
        }
        Page<McaFile> page = new Page<>(request.getPageNum(), request.getPageSize());
        SortUtil.handlePageSort(request, page, true);
        return this.page(page,queryWrapper);
    }

    @Override
    public List<McaFile> findMcaList() {
        LambdaQueryWrapper<McaFile> queryWrapper = new LambdaQueryWrapper<>();
        return this.list(queryWrapper);
    }

    @Override
    @Transactional
    public Boolean createMca(McaCreateDto mcaCreateDto) throws MttErpException {
        McaFile mcaFile = McaFile.builder().build();
        BeanUtils.copyProperties(mcaCreateDto,mcaFile);
        try {
            return this.save(mcaFile);
        }catch (Exception e){
            log.error("新增快递信息失败");
            throw new MttErpException("新增快递信息失败");
        }
    }

    @Override
    @Transactional
    public Boolean updateMca(McaFile mcaFile) throws MttErpException {
        try {
            return this.updateById(mcaFile);
        }catch (Exception e){
            log.error("修改快递信息失败");
            throw new MttErpException("修改快递信息失败");
        }
    }

    @Override
    @Transactional
    public Boolean deleteMca(String[] mcaIds) throws MttErpException {
        try {
            //执行批量删除
            int i = this.baseMapper.deleteBatchIds(Arrays.asList(mcaIds));
            // 判断影响行数，如果为0说明删掉了不存在的记录
            if (i>0) {
                return true;
            }else{
                return false;
            }
        }catch (Exception e){
            log.error("删除快递信息失败");
            throw new MttErpException("删除快递信息失败");
        }
    }

    @Override
    public McaFile seekMca(String id) {
        return this.getById(id);
    }
}
